Imports System.IO

Public Class Form1

    Private db As Database = Database.db
    Private users() As User
    Private selectedUser As String
    Private files() As String
    Private hasFiles As Boolean = False

    Public Event bothSelected()

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        frmLogin.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        db.progress = Me.progress
        Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
        Dim keyFile() As String = IO.Directory.GetFiles(folder, "*.key")
        For i As Integer = 0 To keyFile.Length - 1
            If Path.GetFileNameWithoutExtension(keyFile(i)) = db.getMyEmail Then
                db.setMyPrivateKey(File.ReadAllBytes(keyFile(i)))
                If Not db.validateMyPrivateKey Then db.setMyPrivateKey(Nothing)
                Exit For
            End If
        Next

        users = db.getUsers
        For i As Integer = 0 To users.Length - 1
            lstUsers.Items.Add(users(i).email)
        Next
        If db.checkForFile Then
            frmFileList.Show()
            If db.getMyPrivateKey Is Nothing Then
                frmPrivateKey.Show()
                frmPrivateKey.BringToFront()
                frmPrivateKey.Focus()
            End If
        Else           
        End If
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Then Return
        ReDim files(1)
        files(0) = OpenFileDialog1.FileName
        lstFileName.Items.Clear()
        lstFileName.Items.Add(Path.GetFileName(files(0)))
        hasFiles = True
        'If (lstFileName.Items.Count < 1) Then
        '    Dim path As String = OpenFileDialog1.FileName
        '    safeFileName = System.IO.Path.GetFileName(path)
        'End If
        If lstUsers.SelectedIndex >= 0 Then
            If hasFiles Then
                RaiseEvent bothSelected()
            End If
        End If
    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        frmLogin.Close()
    End Sub

    Private Sub btnReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceive.Click
        frmFileList.Show()
    End Sub

    Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged
        selectedUser = lstUsers.SelectedItem
        If lstUsers.SelectedIndex >= 0 Then
            If hasFiles Then
                RaiseEvent bothSelected()
            End If
        End If        
    End Sub

    Private Sub readyToSend() Handles Me.bothSelected
        If files.Length < 1 Then Return
        If MsgBox("Are you sure you want to send the file(s) to " & selectedUser & "?", vbYesNo) = vbYes Then
            Me.Enabled = False
            frmFileList.Enabled = False
            For i As Integer = 0 To files.Length - 1
                progress.Value = i
                db.uploadFile(files(i), users(lstUsers.SelectedIndex).id)
                lstFileName.Items.RemoveAt(0)
            Next
            Me.Enabled = True
            frmFileList.Enabled = True
            Array.Resize(files, 0)
            hasFiles = False
            lstFileName.Items.Clear()

            'If db.uploadFile(lstFileName.Items.Item(0), users(lstUsers.SelectedIndex).id) Then
            '    MsgBox("file upload successful")
            'Else
            '    MsgBox("upload failed")
            'End If
        End If
    End Sub

    'Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Dim iResponce As Integer = vbYes
    '    'iResponce = MsgBox("Are You Sure you want to send the file to " & selectedUser & "?", vbYesNo)
    '    If iResponce = vbYes Then ' They Clicked YES!
    '        If db.uploadFile(lstFileName.Text, users(lstUsers.SelectedIndex).id) Then
    '            MsgBox("file upload successful")
    '        Else
    '            MsgBox("upload failed")
    '        End If
    '    End If
    'End Sub

    Private Sub lstFileName_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFileName.DragDrop
        If e.Data.GetDataPresent("FileDrop") Then
            lstFileName.Items.Clear()
            files = CType(e.Data.GetData("FileDrop", True), String())
            For Each theFile As String In files
                Dim info As New FileInfo(theFile)
                lstFileName.Items.Add(Path.GetFileName(theFile) & " (" & Math.Round(CDbl(info.Length) / 1024 / 1024, 2) & " MB)")
                lstFileName.SelectedIndex = 0
            Next
            hasFiles = True
        End If
        If lstUsers.SelectedIndex >= 0 Then
            If hasFiles Then
                RaiseEvent bothSelected()
            End If
        End If
    End Sub

    Private Sub lstFileName_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFileName.DragOver
        If e.Data.GetDataPresent("FileDrop") Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        lstFileName.Items.Clear()
        lstUsers.SelectedIndex = -1
    End Sub

    Private Sub lstFileName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFileName.SelectedIndexChanged
        lstFileName.SelectedIndex = -1
    End Sub
End Class